import router, { constantRoutes, asyncRouter } from '@/router'

const state = {
  routes: constantRoutes
}
const mutations = {
  setRoutes(state, newRoutes) { // 静态路由 + 根据权限动态路由
    state.routes = [...constantRoutes, ...newRoutes]
    // state.routes.push()
  }
}
// 回顾之前的思路
// 根据menus权限过滤出符合条件的动态路由
const actions = {
  filterRouters(context, menus) {
    const realRotues = asyncRouter.filter(ele => {
      return menus.includes(ele.code)
    })
    context.commit('setRoutes', realRotues)
    console.log(realRotues)
    router.addRoutes([...realRotues, { path: '*', redirect: '/404', hidden: true }])
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
